#include "Melle.h"
#include "RockFall.h"

void Melle::update(float frameTime)
{
	/** Check collision with eneimes */
	std::list<Entity*>* enemies = EntityManager::getNamedEntities("Enemy");
	if (enemies->size() > 0)
	{
		D3DSURFACE_DESC hitDesc;
		texture->GetLevelDesc(0, &hitDesc);
		D3DXVECTOR3 hitSize(hitDesc.Width, hitDesc.Height, 0);
		BoundingBox hitBox = { position, hitSize };

		std::list<Entity*>::iterator iter = enemies->begin();
		for (; iter != enemies->end(); iter++)
		{
			if ((*iter)->isAlive())
			{
				D3DSURFACE_DESC enemyDesc;
				enemies->front()->getTexture()->GetLevelDesc(0, &enemyDesc);
				D3DXVECTOR3 enemySize(enemyDesc.Width, enemyDesc.Height, 0);
				BoundingBox enemyBox = { ((*iter)->getPosition() + LevelManager::getCurrentLevel()->getOffset()), enemySize };
				if (TestCollision(hitBox, enemyBox))
				{
					alive = false;
					Message message((*iter), "Die", 0);
					MessageHandler::sendMessage(message);
				}
			}
		}
	}
}

void Melle::draw(ID3DXSprite* sprite)
{
	D3DXVECTOR3 tempPos = position - LevelManager::getCurrentLevel()->getOffset();
	if (alive)
		sprite->Draw(texture, 0, 0, &tempPos, D3DCOLOR_XRGB(255, 255, 255));
}